<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class Period.
 *
 * @package namespace App\Models;
 */
class Period extends BaseModel implements Transformable
{
    use TransformableTrait;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['year', 'month', 'name', 'start_date', 'end_date', 'status'];

    public function getPeriodDaySpanAttribute()
    {
        $start = Carbon::createFromFormat('Y-m-d', $this->start_date);
        $end = Carbon::createFromFormat('Y-m-d', $this->end_date);
        return $end->diffInDays($start) + 1;
    }

    public function dormMeterRecords()
    {
        return $this->hasMany(DormMeterRecord::class);
    }

    public function next()
    {
        return $this->where('year', $this->year)->where('month', $this->month + 1)->first();
    }
}
